import Layout from "@/layout"; //养殖系统
import Layout_second from "@/layout-second"; //养殖系统
import Layout_oms from "@/layout-oms";//运营管理
import Layout_tss from "@/layout-tss"; //溯源系统
import userService from "@/api/system-settings/user-manage"

import { asyncRoutes, constantRoutes } from "@/router";
import { getToken } from "@/utils/auth";

const _import = require("../../router/_import_" + process.env.NODE_ENV); // 获取组件的方法
function filterAsyncRouter (asyncRouterMap) {
  // 遍历后台传来的路由字符串，转换为组件对象
  const accessedRouters = asyncRouterMap.filter((route) => {
    if (route.component) {
      // Layout组件特殊处理
      if (route.component === "Layout") { //运营管理系统
        route.component = Layout;
      } else if (route.component === "Layout_second") { //二级路由
        route.component = Layout_second;
      } else if (route.component === "Layout-oms") { //智慧渔业
        route.component = Layout_oms;
      } else if (route.component === "Layout-tss") { //溯源
        route.component = Layout_tss;
      } else {
        if (typeof (route.component) === 'string') {
          route.component = _import(route.component);
        } else {
          console.log(route.component)
        }
        // // route.component = _import(route.component);
        // if (JSON.stringify(route.component).indexOf("{}") !== -1) {
        //   // console.log(route.component)
        // } else {
        //   route.component = _import(route.component);
        // }
      }
    }
    if (route.children) {
      route.children = filterAsyncRouter(route.children);
    }
    return true;
  });

  return accessedRouters;
}
// function componentuser (url) {
//   import(url)
// }
const state = {
  routes: [],
  addRoutes: [],
  pageElementList: [],
  _axiosPromiseArr: []
};
const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes;
    state.routes = constantRoutes.concat(routes);
  },
  SET_PAGE_ELEMENT: (state, payload) => {
    state.pageElementList = payload;
  },
  add_AxiosPromiseArr (state, item) {
    state._axiosPromiseArr.push(item)
  },
  clear_AxiosPromiseArr (state) {
    state._axiosPromiseArr = []
  }
};

const getters = {
  judgePermission: state => {
    return (pageNo, code) => {
      return true;
    }
  }
}
// 是否启用动态路由 true false
const nowmenutype = false;

const actions = {
  generateRoutes ({ commit }) {
    return new Promise(async (resolve) => {
      commit("SET_ROUTES", []);
      let accessedRoutes;
      let virtualSystemNo = getToken("userId");
      if (nowmenutype) {
        let res = await userService.queryAllPermissionList({ userId: virtualSystemNo, funcType: 'WEB', })
        console.log('rputer', res)
        let testjk = [];
        let zhrouter = res.data.functionPermissionMap.WEB || [];
        testjk.push(...zhrouter);
        console.log(testjk)
        let router404 = {
          path: "*",
          redirect: "/404",
          hidden: true
        };

        testjk.push(router404);
        accessedRoutes = filterAsyncRouter(testjk);
        console.log(accessedRoutes, 'accessedRoutes')
        commit("SET_ROUTES", accessedRoutes);
        resolve(accessedRoutes);
      } else {
        let testjk = [];
        // 新增一个最后的404路由
        let router404 = {
          path: "*",
          redirect: "/404",
          hidden: true
        };

        testjk.push(...asyncRoutes);
        testjk.push(router404);
        accessedRoutes = filterAsyncRouter(testjk);
        console.log('luyou',accessedRoutes)
        commit("SET_ROUTES", accessedRoutes);
        resolve(accessedRoutes);
      }
    });
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  getters,
  nowmenutype,
  actions
};
